﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using YXShop.IDAL.Product;

namespace YXShop.SQLServerDAL.Product
{
    /// <summary>
    /// 数据访问类ProductType。
    /// </summary>
    public class ProductType :DbBase,IProductType
    {
        public ProductType()
        { }
        #region "DataBase Operation"

        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add(YXShop.Model.Product.ProductType model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into "+Pre+"producttype(");
            strSql.Append("TypeName,TypeAliasName,IsEntityProduct,RelevanceBrand,ProperyTableName,ParameterTableName,BuyOptionSetTableName,UpdateTime)");
            strSql.Append(" values (");
            strSql.Append("@TypeName,@TypeAliasName,@IsEntityProduct,@RelevanceBrand,@ProperyTableName,@ParameterTableName,@BuyOptionSetTableName,@UpdateTime)");
            strSql.Append(";select @@IDENTITY");
            SqlParameter[] paras = (SqlParameter[])this.ValueParas(model);
            object obj = ChangeHope.DataBase.SQLServerHelper.GetSingle(strSql.ToString(), paras);
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
        /// <summary>
        /// 增加商品类型（商品属性）
        /// </summary>
        /// <param name="model"></param>
        public void AddProductProperty(YXShop.Model.Product.ProductTypeProperty model)
        {
            SqlParameter[] parameters = (SqlParameter[])this.PropertyValueParas(model);
            int reVaule = 0;
            object obj = ChangeHope.DataBase.SQLServerHelper.RunProcedure("sp_Insert_ProductProperty", parameters, out reVaule);
        }
        /// <summary>
        /// 增加商品类型（商品参数）
        /// </summary>
        /// <param name="model"></param>
        public void AddProductParameter(YXShop.Model.Product.ProductTypeParameter model)
        {
            SqlParameter[] parameters = (SqlParameter[])this.ParameterValueParas(model);
            int reVaule = 0;
            object obj = ChangeHope.DataBase.SQLServerHelper.RunProcedure("sp_Insert_ProductParameter", parameters, out reVaule);
        }
        /// <summary>
        /// 增加商品类型（用户购买必需项）
        /// </summary>
        /// <param name="model"></param>
        public void AddProductBuySet(YXShop.Model.Product.ProductTypeBuySet model)
        {
            SqlParameter[] parameters = (SqlParameter[])this.BuySetValueParas(model);
            int reVaule = 0;
            object obj = ChangeHope.DataBase.SQLServerHelper.RunProcedure("sp_Insert_ProductBuySet", parameters, out reVaule);
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public int Update(YXShop.Model.Product.ProductType model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update "+Pre+"producttype set ");
            strSql.Append("TypeName=@TypeName,");
            strSql.Append("TypeAliasName=@TypeAliasName,");
            strSql.Append("IsEntityProduct=@IsEntityProduct,");
            strSql.Append("RelevanceBrand=@RelevanceBrand,");
            strSql.Append("ProperyTableName=@ProperyTableName,");
            strSql.Append("ParameterTableName=@ParameterTableName,");
            strSql.Append("BuyOptionSetTableName=@BuyOptionSetTableName,");
            strSql.Append("UpdateTime=@UpdateTime");
            string sequel = strSql.ToString() + UpdateWhereSequel;
            object obj = ChangeHope.DataBase.SQLServerHelper.ExecuteSql(sequel, (SqlParameter[])this.ValueParas(model));
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
        /// <summary>
        /// 更新一条数据(商品属性)
        /// </summary>
        public int UpdateProductProperty(YXShop.Model.Product.ProductTypeProperty model,int productTypeId)
        {
            SqlParameter[] paras = new SqlParameter[9];
            paras[0] = new SqlParameter("@TypeID", SqlDbType.Int, 4);
            paras[1] = new SqlParameter("@Prefix", SqlDbType.VarChar, 10);
            paras[2] = new SqlParameter("@PropertyName", SqlDbType.VarChar, 100);
            paras[3] = new SqlParameter("@PropertyEnglishName", SqlDbType.VarChar, 200);
            paras[4] = new SqlParameter("@PropertyType", SqlDbType.Int, 4);
            paras[5] = new SqlParameter("@PropertyValue", SqlDbType.VarChar, 1000);
            paras[6] = new SqlParameter("@Sort", SqlDbType.Int, 4);
            paras[7] = new SqlParameter("@Id", SqlDbType.Int, 4);
            paras[8] = new SqlParameter("@PropertyClass",SqlDbType.Int,4);
            paras[0].Value = model.TypeID;
            paras[1].Value = Pre;
            paras[2].Value = model.PropertyName;
            paras[3].Value = model.PropertyEnglishName;
            paras[4].Value = model.PropertyType;
            paras[5].Value = model.PropertyValue;
            paras[6].Value = model.Sort;
            paras[7].Value = model.ID;
            paras[8].Value = model.PropertyClass;
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update " + Pre + "productproperty_" + productTypeId + " set ");
            strSql.Append("PropertyName=@PropertyName,");
            strSql.Append("PropertyEnglishName=@PropertyEnglishName,");
            strSql.Append("PropertyType=@PropertyType,");
            strSql.Append("PropertyValue=@PropertyValue,");
            strSql.Append("PropertyClass=@PropertyClass,");
            strSql.Append("Sort=@Sort");
            string sequel = strSql.ToString() + UpdateWhereSequel;
            object obj = ChangeHope.DataBase.SQLServerHelper.ExecuteSql(sequel, paras);
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
        /// <summary>
        /// 更新一条数据(商品参数)
        /// </summary>
        public int UpdateProductParameter(YXShop.Model.Product.ProductTypeParameter model, int productTypeId)
        {
            SqlParameter[] paras = new SqlParameter[6];
            paras[0] = new SqlParameter("@Id", SqlDbType.Int, 4);
            paras[1] = new SqlParameter("@ParID", SqlDbType.Int, 4);
            paras[2] = new SqlParameter("@ParameterName", SqlDbType.VarChar, 100);
            paras[3] = new SqlParameter("@ParameterType", SqlDbType.Int, 4);
            paras[4] = new SqlParameter("@ParameterValue", SqlDbType.VarChar, 1000);
            paras[5] = new SqlParameter("@Sort", SqlDbType.Int, 4);

            paras[0].Value = model.ID;
            paras[1].Value = model.ParID;
            paras[2].Value = model.ParameterName;
            paras[3].Value = model.ParameterType;
            paras[4].Value = model.ParameterValue;
            paras[5].Value = model.Sort;
            
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update " + Pre + "productparameter_" + productTypeId + " set ");
            strSql.Append("ParID=@ParID,");
            strSql.Append("ParameterName=@ParameterName,");
            strSql.Append("ParameterType=@ParameterType,");
            strSql.Append("ParameterValue=@ParameterValue,");
            strSql.Append("Sort=@Sort");
            string sequel = strSql.ToString() + UpdateWhereSequel;
            object obj = ChangeHope.DataBase.SQLServerHelper.ExecuteSql(sequel, paras);
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
        /// <summary>
        /// 更新一条数据(商品参数)
        /// </summary>
        public int UpdateProductBuySet(YXShop.Model.Product.ProductTypeBuySet model, int productTypeId)
        {
            SqlParameter[] paras = new SqlParameter[5];
            paras[0] = new SqlParameter("@Id", SqlDbType.Int, 4);
            paras[1] = new SqlParameter("@BuySetName", SqlDbType.VarChar, 100);
            paras[2] = new SqlParameter("@BuySetType", SqlDbType.Int, 4);
            paras[3] = new SqlParameter("@BuySetValue", SqlDbType.VarChar, 1000);
            paras[4] = new SqlParameter("@Sort", SqlDbType.Int, 4);

            paras[0].Value = model.ID;
           
            paras[1].Value = model.BuySetName;
            paras[2].Value = model.BuySetType;
            paras[3].Value = model.BuySetValue;
            paras[4].Value = model.Sort;

            StringBuilder strSql = new StringBuilder();
            strSql.Append("update " + Pre + "productbuyset_" + productTypeId + " set ");
            strSql.Append("BuySetName=@BuySetName,");
            strSql.Append("BuySetType=@BuySetType,");
            strSql.Append("BuySetValue=@BuySetValue,");
            strSql.Append("Sort=@Sort");
            string sequel = strSql.ToString() + UpdateWhereSequel;
            object obj = ChangeHope.DataBase.SQLServerHelper.ExecuteSql(sequel, paras);
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
        /// <summary>
        /// 删除一条数据
        /// </summary>
        public void Delete(int Id)
        {
            SqlParameter[] paras = new SqlParameter[2];
            paras[0] = new SqlParameter("@TypeID", SqlDbType.Int, 4);
            paras[1] = new SqlParameter("@Prefix", SqlDbType.VarChar, 10);
            paras[0].Value = Id;
            paras[1].Value = Pre;
            int reVaule = 0;
            object obj = ChangeHope.DataBase.SQLServerHelper.RunProcedure("sp_Del_ProductType", paras, out reVaule);
        }
        /// <summary>
        /// 删除一条数据(商品属性)
        /// </summary>
        public void DeleteProductProperty(int id,int productTypeId)
        {
            string strSql="delete "+ Pre + "productproperty_" + productTypeId + ""+this.UpdateWhereSequel;
             ChangeHope.DataBase.SQLServerHelper.ExecuteSql(strSql.ToString(), (SqlParameter[])this.ValueIDPara(id));
        }
        /// <summary>
        /// 删除一条数据(商品参数)
        /// </summary>
        public int DeleteProductParameter(int id, int productTypeId)
        {
            string strSql = "delete " + Pre + "productparameter_" + productTypeId + "" + this.UpdateWhereSequel;
            object obj = ChangeHope.DataBase.SQLServerHelper.ExecuteSql(strSql.ToString(), (SqlParameter[])this.ValueIDPara(id));
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
        /// <summary>
        /// 删除一条数据(用户购买必需项)
        /// </summary>
        public int DeleteProductBuySet(int id, int productTypeId)
        {
            string strSql = "delete " + Pre + "productbuyset_" + productTypeId + "" + this.UpdateWhereSequel;
            object obj = ChangeHope.DataBase.SQLServerHelper.ExecuteSql(strSql.ToString(), (SqlParameter[])this.ValueIDPara(id));
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
        /// <summary>
        /// 修改任意字段
        /// </summary>
        /// <param name="id"></param>
        /// <param name="columnName"></param>
        /// <param name="value"></param>
        /// <returns></returns>
        public int Amend(int id, string columnName, Object value)
        {
            string sequel = "Update [" + Pre + "producttype] set ";
            sequel = sequel + "[" + columnName + "] =@Value ";
            sequel = sequel + UpdateWhereSequel;
            SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@Value", value), new SqlParameter("@Id", id) };
            object obj = ChangeHope.DataBase.SQLServerHelper.ExecuteSql(sequel, paras);
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
        #endregion

        #region "Data Load"
        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public YXShop.Model.Product.ProductType GetModel(int Id)
        {
            string strSql=this.SelectSequel+this.UpdateWhereSequel;
            YXShop.Model.Product.ProductType model = new YXShop.Model.Product.ProductType();
            DataSet ds = ChangeHope.DataBase.SQLServerHelper.Query(strSql, (SqlParameter[])this.ValueIDPara(Id));
            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["Id"].ToString() != "")
                {
                    model.Id = int.Parse(ds.Tables[0].Rows[0]["Id"].ToString());
                }
                model.TypeName = ds.Tables[0].Rows[0]["TypeName"].ToString();
                model.TypeAliasName = ds.Tables[0].Rows[0]["TypeAliasName"].ToString();
                if (ds.Tables[0].Rows[0]["IsEntityProduct"].ToString() != "")
                {
                    model.IsEntityProduct = int.Parse(ds.Tables[0].Rows[0]["IsEntityProduct"].ToString());
                }
                model.RelevanceBrand = ds.Tables[0].Rows[0]["RelevanceBrand"].ToString();
                model.ProperyTableName = ds.Tables[0].Rows[0]["ProperyTableName"].ToString();
                model.ParameterTableName = ds.Tables[0].Rows[0]["ParameterTableName"].ToString();
                model.BuyOptionSetTableName = ds.Tables[0].Rows[0]["BuyOptionSetTableName"].ToString();
                if (ds.Tables[0].Rows[0]["UpdateTime"].ToString() != "")
                {
                    model.UpdateTime = DateTime.Parse(ds.Tables[0].Rows[0]["UpdateTime"].ToString());
                }
                return model;
            }
            else
            {
                return null;
            }
        }
        /// <summary>
        /// 商品类型商品属性得到一个对象实体(商品属性)
        /// </summary>
        public YXShop.Model.Product.ProductTypeProperty GetProductPropertyModel(int Id, int productTypeId)
        {
            string strSql = "select Id,PropertyName,PropertyEnglishName,PropertyType,PropertyValue,Sort from " + Pre + "productproperty_" + productTypeId + " where Id=@Id";
            YXShop.Model.Product.ProductTypeProperty model = new YXShop.Model.Product.ProductTypeProperty();
            DataSet ds = ChangeHope.DataBase.SQLServerHelper.Query(strSql, (SqlParameter[])this.ValueIDPara(Id));
            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["Id"].ToString() != "")
                {
                    model.ID = int.Parse(ds.Tables[0].Rows[0]["Id"].ToString());
                }
                model.PropertyName = ds.Tables[0].Rows[0]["PropertyName"].ToString();
                model.PropertyEnglishName = ds.Tables[0].Rows[0]["PropertyEnglishName"].ToString();
                if (ds.Tables[0].Rows[0]["PropertyType"].ToString() != "")
                {
                    model.PropertyType = int.Parse(ds.Tables[0].Rows[0]["PropertyType"].ToString());
                }
                model.PropertyValue = ds.Tables[0].Rows[0]["PropertyValue"].ToString();
                if (ds.Tables[0].Rows[0]["Sort"].ToString() != "")
                {
                    model.Sort = int.Parse(ds.Tables[0].Rows[0]["Sort"].ToString());
                }
                return model;
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 商品类型商品属性得到一个对象实体(商品参数)
        /// </summary>
        public YXShop.Model.Product.ProductTypeParameter GetProductParameterModel(int Id, int productTypeId)
        {
            string strSql = "select Id,ParID,ParameterName,ParameterType,ParameterValue,Sort from " + Pre + "productparameter_" + productTypeId + " where Id=@Id";
            YXShop.Model.Product.ProductTypeParameter model = new YXShop.Model.Product.ProductTypeParameter();
            DataSet ds = ChangeHope.DataBase.SQLServerHelper.Query(strSql, (SqlParameter[])this.ValueIDPara(Id));
            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["Id"].ToString() != "")
                {
                    model.ID = int.Parse(ds.Tables[0].Rows[0]["Id"].ToString());
                }
                model.ParameterName = ds.Tables[0].Rows[0]["ParameterName"].ToString();
                if (ds.Tables[0].Rows[0]["ParID"].ToString() != "")
                {
                    model.ParID = int.Parse(ds.Tables[0].Rows[0]["ParID"].ToString());
                }
                if (ds.Tables[0].Rows[0]["ParameterType"].ToString() != "")
                {
                    model.ParameterType = int.Parse(ds.Tables[0].Rows[0]["ParameterType"].ToString());
                }
                model.ParameterValue = ds.Tables[0].Rows[0]["ParameterValue"].ToString();
                if (ds.Tables[0].Rows[0]["Sort"].ToString() != "")
                {
                    model.Sort = int.Parse(ds.Tables[0].Rows[0]["Sort"].ToString());
                }
                return model;
            }
            else
            {
                return null;
            }
        }
        /// <summary>
        /// 商品类型商品属性得到一个对象实体(用户购买必需项)
        /// </summary>
        public YXShop.Model.Product.ProductTypeBuySet GetProductBuySetModel(int Id, int productTypeId)
        {
            string strSql = "select Id,BuySetName,BuySetType,BuySetValue,Sort from " + Pre + "productbuyset_" + productTypeId + " where Id=@Id";
            YXShop.Model.Product.ProductTypeBuySet model = new YXShop.Model.Product.ProductTypeBuySet();
            DataSet ds = ChangeHope.DataBase.SQLServerHelper.Query(strSql, (SqlParameter[])this.ValueIDPara(Id));
            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["Id"].ToString() != "")
                {
                    model.ID = int.Parse(ds.Tables[0].Rows[0]["Id"].ToString());
                }
                model.BuySetName = ds.Tables[0].Rows[0]["BuySetName"].ToString();
                if (ds.Tables[0].Rows[0]["BuySetType"].ToString() != "")
                {
                    model.BuySetType = int.Parse(ds.Tables[0].Rows[0]["BuySetType"].ToString());
                }
                model.BuySetValue = ds.Tables[0].Rows[0]["BuySetValue"].ToString();
                if (ds.Tables[0].Rows[0]["Sort"].ToString() != "")
                {
                    model.Sort = int.Parse(ds.Tables[0].Rows[0]["Sort"].ToString());
                }
                return model;
            }
            else
            {
                return null;
            }
        }
        /// <summary>
        /// 所有记录
        /// </summary>
        /// <remarks></remarks>
        public ChangeHope.DataBase.DataByPage GetList()
        {
            ChangeHope.DataBase.DataByPage dataPage = new ChangeHope.DataBase.DataByPage();
            dataPage.Sql = "[select] Id,TypeName,TypeAliasName,IsEntityProduct,RelevanceBrand,ProperyTableName,ParameterTableName,BuyOptionSetTableName,UpdateTime [from] " + Pre + "producttype [where] 1=1 [order by] Id asc";
            dataPage.GetRecordSetByPage();
            return dataPage;
        }
        /// <summary>
        /// 获取属性表名与品牌
        /// </summary>
        /// <param name="cid">商品分类ID</param>
        /// <returns></returns>
        public DataTable GetProductTypeBrandAndProperty(int cid)
        {
            string sql = "Select Id,RelevanceBrand,ProperyTableName from " + Pre + "producttype where id=(Select productTypeId from " + Pre + "productclass where cid=@cid)";
            SqlParameter[] paras = new SqlParameter[1];
            paras[0] = new SqlParameter("@cid", cid);
            paras[0].DbType = DbType.Int32;
            DataTable dt = ChangeHope.DataBase.SQLServerHelper.Query(sql, paras).Tables[0];
            return dt;
        }
        /// <summary>
        /// 商品类型(商品属性)
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ChangeHope.DataBase.DataByPage GetProductPropertyList(int id)
        {
            ChangeHope.DataBase.DataByPage dataPage = new ChangeHope.DataBase.DataByPage();
            dataPage.Sql = "[select] Id,PropertyName,PropertyEnglishName,PropertyType,PropertyValue,Sort [from] " + Pre + "productproperty_" + id + " [where] 1=1 [order by] Id asc";
            dataPage.GetRecordSetByPage();
            return dataPage;
        }
        /// <summary>
        /// 根据条件得到List列表(商品属性)
        /// </summary>
        /// <param name="productTypeId"></param>
        /// <returns></returns>
        public List<YXShop.Model.Product.ProductTypeProperty> GetProductPropertyAllByWhere(int productTypeId,string strWhere)
        {
            List<YXShop.Model.Product.ProductTypeProperty> list = new List<YXShop.Model.Product.ProductTypeProperty>();
            StringBuilder strSql = new StringBuilder();
            strSql.Append("if exists(select * from syscolumns where id=object_id('" + Pre + "productproperty_" + productTypeId + "'))");
            strSql.Append("select Id,PropertyName,PropertyEnglishName,PropertyType,PropertyValue,Sort from " + Pre + "productproperty_"+productTypeId+" ");
            if (strWhere != null && strWhere != "")
            {
                strSql.Append("where " + strWhere + " ");
            }

            using (SqlDataReader reader = ChangeHope.DataBase.SQLServerHelper.ExecuteReader(strSql.ToString()))
            {
                while (reader.Read())
                {
                    YXShop.Model.Product.ProductTypeProperty model = new YXShop.Model.Product.ProductTypeProperty();
                    model.ID = Convert.ToInt32(reader["Id"].ToString());
                    model.PropertyName = reader["PropertyName"].ToString();
                    model.PropertyEnglishName = reader["PropertyEnglishName"].ToString();
                    model.PropertyType = Convert.ToInt32(reader["PropertyType"].ToString());
                    model.PropertyValue = reader["PropertyValue"].ToString();
                    model.Sort = Convert.ToInt32(reader["Sort"].ToString());
                    list.Add(model);
                }
            }
            return list;
        }
        /// <summary>
        /// 根据条件得到List列表(商品参数)
        /// </summary>
        /// <param name="productTypeId"></param>
        /// <returns></returns>
        public List<YXShop.Model.Product.ProductTypeParameter> GetProductParameterAllByWhere(int productTypeId, string strWhere)
        {
            List<YXShop.Model.Product.ProductTypeParameter> list = new List<YXShop.Model.Product.ProductTypeParameter>();
            StringBuilder strSql = new StringBuilder();
            strSql.Append("if exists(select * from syscolumns where id=object_id('" + Pre + "productparameter_" + productTypeId + "'))");
            strSql.Append("select Id,ParID,ParameterName,ParameterType,ParameterValue,Sort from " + Pre + "productparameter_" + productTypeId + " ");
            if (strWhere != null && strWhere != "")
            {
                strSql.Append("where " + strWhere + " ");
            }
            using (SqlDataReader reader = ChangeHope.DataBase.SQLServerHelper.ExecuteReader(strSql.ToString()))
            {
                while (reader.Read())
                {
                    YXShop.Model.Product.ProductTypeParameter model = new YXShop.Model.Product.ProductTypeParameter();
                    model.ID = Convert.ToInt32(reader["Id"].ToString());
                    model.ParameterName = reader["ParameterName"].ToString();
                    model.ParameterValue = reader["ParameterValue"].ToString();
                    model.ParameterType = Convert.ToInt32(reader["ParameterType"].ToString());
                    model.ParID =Convert.ToInt32(reader["ParID"].ToString());
                    model.Sort = Convert.ToInt32(reader["Sort"].ToString());
                    list.Add(model);
                }
            }
            return list;
        }
        /// <summary>
        /// 根据条件得到列表(商品类型)
        /// </summary>
        /// <param name="strWhere"></param>
        /// <returns></returns>
        public List<YXShop.Model.Product.ProductType> GetAllByWhere(string strWhere)
        {
            List<YXShop.Model.Product.ProductType> list = new List<YXShop.Model.Product.ProductType>();
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select Id,TypeName,TypeAliasName,IsEntityProduct,RelevanceBrand from " + Pre + "producttype ");
            if (strWhere != null && strWhere != "")
            {
                strSql.Append("where " + strWhere + " ");
            }

            using (SqlDataReader reader = ChangeHope.DataBase.SQLServerHelper.ExecuteReader(strSql.ToString()))
            {
                while (reader.Read())
                {
                    YXShop.Model.Product.ProductType model = new YXShop.Model.Product.ProductType();
                    model.Id =Convert.ToInt32(reader["Id"].ToString());
                    model.TypeName = reader["TypeName"].ToString();
                    model.TypeAliasName = reader["TypeAliasName"].ToString();
                    model.IsEntityProduct = Convert.ToInt32(reader["IsEntityProduct"].ToString());
                    model.RelevanceBrand = reader["RelevanceBrand"].ToString();
                    list.Add(model);
                }
            }
            return list;
        }
        /// <summary>
        /// 商品类型(商品参数)
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ChangeHope.DataBase.DataByPage GetProductParameterList(int id, int SeriesId)
        {
            ChangeHope.DataBase.DataByPage dataPage = new ChangeHope.DataBase.DataByPage();
            dataPage.Sql = "[select] Id,ParID,ParameterName,ParameterType,ParameterValue,Sort [from] " + Pre + "productparameter_" + id + " [where] 1=1 and ParID=" + SeriesId + " [order by] Sort asc";
            dataPage.GetRecordSetByPage();
            return dataPage;
        }
        /// <summary>
        /// 商品类型(用户购买必需项)
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ChangeHope.DataBase.DataByPage GetProductBuySetList(int id)
        {
            ChangeHope.DataBase.DataByPage dataPage = new ChangeHope.DataBase.DataByPage();
            dataPage.Sql = "[select] Id,BuySetName,BuySetType,BuySetValue,Sort [from] " + Pre + "productbuyset_" + id + " [where] 1=1 [order by] Sort asc";
            dataPage.GetRecordSetByPage();
            return dataPage;
        }
        #endregion

        #region "Other function"
        string selectSequel = string.Empty;
        protected string SelectSequel
        {
            get
            {
                if (selectSequel == string.Empty)
                    selectSequel = "Select Id,TypeName,TypeAliasName,IsEntityProduct,RelevanceBrand,ProperyTableName,ParameterTableName,BuyOptionSetTableName,UpdateTime,1 PersistStatus  From [" + Pre + "producttype] ";
                return selectSequel;
            }
            set
            {
                this.selectSequel = value;
            }
        }
        /// <summary>
        /// 
        /// </summary>
        protected string UpdateWhereSequel
        {
            get
            {
                return " Where [Id] = @Id";
            }
        }
        /// <summary>
        /// 该数据访问对象的属性值装载到数据库更新参数数组
        /// </summary>
        /// <remarks></remarks>

        public IDbDataParameter[] ValueParas(YXShop.Model.Product.ProductType model)
        {
            SqlParameter[] paras = new SqlParameter[9];

            paras[0] = new SqlParameter("@Id", SqlDbType.Int, 4);
            paras[1] = new SqlParameter("@TypeName", SqlDbType.VarChar, 50);
            paras[2] = new SqlParameter("@TypeAliasName", SqlDbType.VarChar, 100);
            paras[3] = new SqlParameter("@IsEntityProduct", SqlDbType.Int, 4);
            paras[4] = new SqlParameter("@RelevanceBrand", SqlDbType.VarChar, 500);
            paras[5] = new SqlParameter("@ProperyTableName", SqlDbType.VarChar, 100);
            paras[6] = new SqlParameter("@ParameterTableName", SqlDbType.VarChar, 100);
            paras[7] = new SqlParameter("@BuyOptionSetTableName", SqlDbType.VarChar, 100);
            paras[8] = new SqlParameter("@UpdateTime", SqlDbType.DateTime);
            paras[0].Value = model.Id;
            paras[1].Value = model.TypeName;
            paras[2].Value = model.TypeAliasName;
            paras[3].Value = model.IsEntityProduct;
            paras[4].Value = model.RelevanceBrand;
            paras[5].Value = model.ProperyTableName;
            paras[6].Value = model.ParameterTableName;
            paras[7].Value = model.BuyOptionSetTableName;
            paras[8].Value = model.UpdateTime;
            return paras;
        }
        /// <summary>
        /// 该数据访问对象的属性值装载到数据库更新参数数组(商品属性)
        /// </summary>
        /// <remarks></remarks>
        public IDbDataParameter[] PropertyValueParas(YXShop.Model.Product.ProductTypeProperty model)
        {
            SqlParameter[] paras = new SqlParameter[8];
            paras[0] = new SqlParameter("@TypeID", SqlDbType.Int, 4);
            paras[1] = new SqlParameter("@Prefix", SqlDbType.VarChar, 10);
            paras[2] = new SqlParameter("@PropertyName", SqlDbType.VarChar, 100);
            paras[3] = new SqlParameter("@PropertyEnglishName", SqlDbType.VarChar, 200);
            paras[4] = new SqlParameter("@PropertyType", SqlDbType.Int, 4);
            paras[5] = new SqlParameter("@PropertyValue", SqlDbType.VarChar, 1000);
            paras[6] = new SqlParameter("@Sort", SqlDbType.Int, 4);
            paras[7] = new SqlParameter("@PropertyClass",SqlDbType.Int,4);
            paras[0].Value = model.TypeID;
            paras[1].Value = Pre;
            paras[2].Value = model.PropertyName;
            paras[3].Value = model.PropertyEnglishName;
            paras[4].Value = model.PropertyType;
            paras[5].Value = model.PropertyValue;
            paras[6].Value = model.Sort;
            paras[7].Value = model.PropertyClass;
            return paras;
        }

        /// <summary>
        /// 该数据访问对象的属性值装载到数据库更新参数数组(商品参数)
        /// </summary>
        /// <remarks></remarks>
        public IDbDataParameter[] ParameterValueParas(YXShop.Model.Product.ProductTypeParameter model)
        {
            SqlParameter[] paras = new SqlParameter[7];
            paras[0] = new SqlParameter("@TypeID", SqlDbType.Int, 4);
            paras[1] = new SqlParameter("@Prefix", SqlDbType.VarChar, 10);
            paras[2] = new SqlParameter("@ParameterName", SqlDbType.VarChar, 100);
            paras[3] = new SqlParameter("@ParameterType", SqlDbType.Int, 4);
            paras[4] = new SqlParameter("@ParameterValue", SqlDbType.VarChar, 1000);
            paras[5] = new SqlParameter("@Sort", SqlDbType.Int, 4);
            paras[6] = new SqlParameter("@ParID", SqlDbType.Int, 4);

            paras[0].Value = model.TypeID;
            paras[1].Value = Pre;
            paras[2].Value = model.ParameterName;
            paras[3].Value = model.ParameterType;
            paras[4].Value = model.ParameterValue;
            paras[5].Value = model.Sort;
            paras[6].Value = model.ParID;
            return paras;
        }
        /// <summary>
        /// 该数据访问对象的属性值装载到数据库更新参数数组(用户购买必需项)
        /// </summary>
        /// <remarks></remarks>
        public IDbDataParameter[] BuySetValueParas(YXShop.Model.Product.ProductTypeBuySet model)
        {
            SqlParameter[] paras = new SqlParameter[6];
           
            paras[0] = new SqlParameter("@Prefix", SqlDbType.VarChar, 10);
            paras[1] = new SqlParameter("@BuySetName", SqlDbType.VarChar, 100);
            paras[2] = new SqlParameter("@BuySetType", SqlDbType.Int, 4);
            paras[3] = new SqlParameter("@BuySetValue", SqlDbType.VarChar, 1000);
            paras[4] = new SqlParameter("@Sort", SqlDbType.Int, 4);
            paras[5] = new SqlParameter("@TypeID", SqlDbType.Int, 4);

            
            paras[0].Value = Pre;
            paras[1].Value = model.BuySetName;
            paras[2].Value = model.BuySetType;
            paras[3].Value = model.BuySetValue;
            paras[4].Value = model.Sort;
            paras[5].Value = model.TypeId;
            return paras;
        }
        public IDbDataParameter[] ValueIDPara(int id)
        {
            SqlParameter[] paras = new SqlParameter[1];
            paras[0] = new SqlParameter("@Id", id);
            paras[0].DbType = DbType.Int32;
            return paras;
        }

        #endregion
    }
}
